package com.itheima.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.po.User;
@Controller
public class UserController {
	/**
	 * 接收页面请求的JSON数据，并返回JSON格式结果
	 */
	@RequestMapping("/testJson")
	@ResponseBody
	public User testJson(@RequestBody User user) {
		// 打印接收的JSON格式数据
		
		System.out.println(user);
		System.out.println(user);
		// 返回JSON格式的响应
		return user;
	}
	
	/**
	 *接收RESTful风格的请求,其接收方式为GET
	 */
	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	@ResponseBody
	public User selectUser(@PathVariable("id") String id){
	    //查看数据接收
	    System.out.println("id="+id);
	    User user=new User();
	    //模拟根据id查询出到用户对象数据
	    if(id.equals("1234")){	    	
	        user.setUsername("tom1");
	        user.setPassword("123456");
	        user.setUsername("tom1");
	        user.setPassword("123456");
	    }else {
	        user.setUsername("jerry");
	        user.setPassword("123");
	    }
	    //返回JSON格式的数据
	    return user;
	}
	
	@RequestMapping(value="/testuser/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> testUser(@PathVariable("id") String id){
		
	    System.out.println("id="+id);
	    User user=new User();
	    //模拟根据id查询出到用户对象数据
	    if(id.equals("1234")){	    	
	        user.setUsername("whf");
	        user.setPassword("123456");
	    }else {
	        user.setUsername("jerry");
	        user.setPassword("123");
	    }
	    //返回JSON格式的数据

	    Map<String,Object> map = new HashMap<>();
		List<User> list = new ArrayList<User>() ;

		for(int i=0;i<20;i++) {
			list.add(user);
		}
		
	    User user1=new User();
	    user1.setUsername("whf");
		user1.setPassword("123456789");
		list.add(user1);
		
	    
	    
		map.put("userno", id);//11 10
		map.put("data", list);//11 10
		map.put("code", 0);
		map.put("count", 20);
		map.put("msg", "跳转");
		System.out.println(map);
		return map;
	}
	

}
